
from fastapi import APIRouter, Query, Body

from core.testcase_core import update_testcase_in_db, delete_testcase_in_db, get_testcases_in_db, add_testcase_in_db
from entity.schema.testcase_schema import caseDetail, UpdateCaseRequest, AddCaseRequest
from utils.base_response import BaseResponse

testcase=APIRouter()

@testcase.put("/updateTestCase")
async def update_testcase(req: UpdateCaseRequest):
    await update_testcase_in_db(req.caseId,req.detail)
    return BaseResponse(code=10000, msg="更新成功")

@testcase.delete("/deleteTestCase")
async def delete_testcase(caseId: str = Query(..., alias="caseId")):
    await delete_testcase_in_db(caseId)
    return BaseResponse(code=10000, msg="删除成功")

@testcase.get("/getTestCase")
async def get_testcase(suiteId: str = Query(..., alias="suiteId")):
    result=await get_testcases_in_db(suiteId)
    return BaseResponse(code=10000, msg="用例信息返回成功",data=result)


@testcase.post("/addTestCase")
async def add_testcase(addreq: AddCaseRequest):
    await add_testcase_in_db(addreq.suiteId,addreq.testcasedetail)
    return BaseResponse(code=10000, msg="添加成功")

#
# from fastapi.responses import Response
# @testcase.get("/downloadTestCases")
# async def download_testcases(test: testDownload):
#     format = test.format
#     file_content = await download_testcases_in_db(test.suiteId, format)
#
#     if file_content:
#         # 根据格式确定MIME类型和文件扩展名
#         if format == 1:  # Word文档
#             content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
#             file_extension = ".docx"
#         elif format == 2:  # Excel文件
#             content_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
#             file_extension = ".xlsx"
#         else:  # CSV文件
#             content_type = "text/csv"
#             file_extension = ".csv"
#         # 返回文件下载响应
#         return Response(
#             content=file_content,
#             media_type=content_type,
#             headers={
#                 "Content-Disposition": f"attachment; filename=testcases{file_extension}"
#             }
#         )
#     else:
#         return BaseResponse(code=20000, msg="下载失败")
